import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import FortendLayout from "../layout/FortendLayout.vue";
import BackendLayout from "../layout/BackendLayout.vue";
import { afterEachHandler, beforeEachHandler } from "./permission";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/test",
      name: "TestPage",
      component: () => import("@/views/login/TestPage.vue"),
    },
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/login",
      name: "LoginPage",
      component: () => import("@/views/login/LoginPage.vue"),
    },
    {
      path: "/fortend",
      name: "fortend",
      component: FortendLayout,
      children: [
        {
          name: "FortendBlogList",
          path: "blog/list",
          component: () => import("@/views/fortend/BlogView.vue"),
        },
      ],
    },
    {
      path: "/backend",
      name: "backend",
      component: BackendLayout,
      children: [
        {
          name: "BackendBlogList",
          path: "blog/list",
          component: () => import("@/views/backend/BlogView.vue"),
        },
        {
          name: "BackendTagList",
          path: "tag/list",
          component: () => import("@/views/backend/TagView.vue"),
        },
      ],
    },
    {
      path: "/403",
      name: "PermissionDeny",
      component: () => import("@/views/errors/PermissionDeny.vue"),
    },
    {
      path: "/404",
      name: "NotFound",
      component: () => import("@/views/errors/NotFound.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      redirect: "/404",
    },
  ],
});

router.beforeEach(beforeEachHandler);
router.afterEach(afterEachHandler);

export default router;
